package com.example.blog.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.blog.dao.ArticleDAO;
import com.example.blog.pojo.Article;

@WebServlet("/addarticle")
public class AddArticleServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter pw = resp.getWriter();

		// 1.
		String title = req.getParameter("title");
		String content = req.getParameter("content");

		// 2.
		ArticleDAO dao = new ArticleDAO();
		Article a = new Article();
		a.setTitle(title);
		a.setContent(content);
		try {
			int result = dao.insert(a);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		pw.print("插入成功");

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}